<template>
	<view>
		<u-navbar :is-back="false" title="　" :border-bottom="false">
			<view class="u-flex u-row-right" style="width: 100%;">
				<view class="camera u-flex u-row-center">
					<u-icon name="camera-fill" color="#000000" size="48"></u-icon>
				</view>
			</view>
		</u-navbar>
		<view class="u-flex user-box u-p-l-30 u-p-r-20 u-p-b-30">
			<view class="u-m-r-10" style="position: relative;">
				<u-avatar :src="userInfo.avatar_url" size="140"></u-avatar>
				<u-icon style="position: absolute;right: 10px;bottom: 0;background: #fff;border-radius: 50%;"
					name="reload" color="#969799" size="28" @click="updateUserInfo"></u-icon>
			</view>
			<view class="u-flex-1">
				<view class="u-font-18 u-p-b-20">{{userInfo.name?userInfo.name:''}}</view>
				<view class="u-font-14 u-tips-color">{{userInfo.bio}}</view>
			</view>
			<view class="u-m-l-10 u-p-10">
				<u-icon name="arrow-right" color="#969799" size="28"></u-icon>
			</view>
		</view>
		<view class="userData flex">
			<view class="data-item flex-1 flex-col flex-y-center" @click="linkTo(userInfo.html_url+'/starred')">
				<view class="number txt-bold600 txt-size32">{{userInfo.stared?userInfo.stared:0}}</view>
				<view class="type txt-color-9 txt-custom">Stars</view>
			</view>
			<view class="data-item flex-1 flex-col flex-y-center" @click="linkTo(userInfo.html_url+'/followers')">
				<view class="number txt-bold600 txt-size32">{{userInfo.followers?userInfo.followers:0}}</view>
				<view class="type txt-color-9 txt-custom">Followers</view>
			</view>
			<view class="data-item flex-1 flex-col flex-y-center" @click="linkTo(userInfo.html_url+'/following')">
				<view class="number txt-bold600 txt-size32">{{userInfo.following?userInfo.following:0}}</view>
				<view class="type txt-color-9 txt-custom">following</view>
			</view>
		</view>


		<view class="u-m-t-20">
			<u-cell-group>
				<u-cell-item icon="https://gitee.com/favicon.ico" title="码云" @click="linkTo(userInfo.html_url)">
				</u-cell-item>
				<u-cell-item icon="weibo-circle-fill" title="微博" color="#f0ad4e" @click="linkTo(userInfo.weibo)">
				</u-cell-item>
				<u-cell-item icon="chrome-circle-fill" title="博客" color="#2b85e4" @click="linkTo(userInfo.blog)">
				</u-cell-item>
			</u-cell-group>
		</view>

		<view class="u-m-t-20" style="padding: 0 20rpx;">
			<u-button type="error" @click="onLogout">退出登录</u-button>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex'
	export default {
		data() {
			return {
				pic: 'https://uviewui.com/common/logo.png',
				show: true
			}
		},
		computed: {
			...mapGetters(['userInfo', 'token'])
		},
		onLoad() {
			if (!this.$store.getters['token']) {
				uni.redirectTo({
					url: '/pages/login/login'
				})
			}
		},
		methods: {
			linkTo(url) {
				if (url) {
					window.location.href = url
				} else {
					uni.showToast({
						icon: 'none',
						title: '暂未绑定'
					})
				}
			},
			updateUserInfo() { //同步更新gitee用户信息
				this.$u.api('user', {
					opt: 'updateUserInfo'
				}).then(res => {
					//todo
				})
			},
			autoLogin(token) { //自动登录
				this.$u.api('user', {
					opt: 'checkToken',
					token
				}).then(res => {
					this.$store.commit('setToken', res.result.data.token)
					this.$store.commit('setUserInfo', res.result.data.userInfo)
				})
			},
			onLogout() {
				let that = this
				uni.showLoading({
					title: '正在退出…'
				})
				setTimeout(() => {
					uni.hideLoading()
					that.$store.commit('setLogout', null)
					uni.redirectTo({
						url: '/pages/login/login'
					})
				}, 1500)
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ededed;
	}

	.userData {
		margin: 0 0 20rpx 0;
		background: #fff;
		padding: 20rpx;
	}

	.camera {
		width: 54px;
		height: 44px;

		&:active {
			background-color: #ededed;
		}
	}

	.user-box {
		background-color: #fff;
	}
</style>
